# ========================================
# RedFire MySQL业务数据库 Dockerfile
# 版本: v5.0.0 (2025-09-16)
# 基于: MySQL 8.0 官方镜像
# 功能: 核心交易业务数据库
# ========================================

FROM mysql:8.0

# 维护者信息
LABEL maintainer="RedFire Team <team@redfire.com>"
LABEL version="5.0.0"
LABEL description="RedFire MySQL Business Database"

# 设置环境变量
ENV MYSQL_ROOT_PASSWORD=redfire_root_2025
ENV MYSQL_DATABASE=redfire_trading
ENV MYSQL_USER=redfire_app
ENV MYSQL_PASSWORD=redfire_app_2025
ENV TZ=Asia/Shanghai

# 安装必要的工具和依赖 (Oracle Linux使用microdnf)
RUN microdnf update -y && microdnf install -y \
    # 网络和系统工具
    curl \
    wget \
    procps-ng \
    # 清理缓存
    && microdnf clean all

# 设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 创建必要的目录
RUN mkdir -p /var/log/mysql \
    && mkdir -p /var/lib/mysql-files \
    && mkdir -p /var/run/mysqld \
    && mkdir -p /docker-entrypoint-initdb.d/scripts \
    && mkdir -p /etc/mysql/conf.d

# 设置目录权限
RUN chown -R mysql:mysql /var/log/mysql \
    && chown -R mysql:mysql /var/lib/mysql-files \
    && chown -R mysql:mysql /var/run/mysqld \
    && chmod 755 /var/log/mysql \
    && chmod 755 /var/lib/mysql-files \
    && chmod 777 /var/run/mysqld

# 复制自定义配置文件并设置正确权限
COPY conf/my.cnf /etc/mysql/conf.d/redfire.cnf
RUN chmod 644 /etc/mysql/conf.d/redfire.cnf && \
    chown mysql:mysql /etc/mysql/conf.d/redfire.cnf

# 复制初始化脚本
COPY scripts/ /docker-entrypoint-initdb.d/scripts/

# 设置脚本权限
RUN chmod +x /docker-entrypoint-initdb.d/scripts/*.sh

# 创建健康检查脚本
RUN echo '#!/bin/bash\n\
mysqladmin ping -h localhost -u root -p${MYSQL_ROOT_PASSWORD} --silent\n\
if [ $? -eq 0 ]; then\n\
    echo "MySQL is healthy"\n\
    exit 0\n\
else\n\
    echo "MySQL is not healthy"\n\
    exit 1\n\
fi' > /usr/local/bin/health-check.sh \
    && chmod +x /usr/local/bin/health-check.sh

# 创建备份脚本
RUN echo '#!/bin/bash\n\
BACKUP_DIR="/var/backups/mysql"\n\
mkdir -p $BACKUP_DIR\n\
DATE=$(date +%Y%m%d_%H%M%S)\n\
mysqldump -u root -p${MYSQL_ROOT_PASSWORD} --all-databases --single-transaction --routines --triggers > $BACKUP_DIR/mysql_backup_$DATE.sql\n\
if [ $? -eq 0 ]; then\n\
    echo "Backup completed: $BACKUP_DIR/mysql_backup_$DATE.sql"\n\
    # 保留最近7天的备份\n\
    find $BACKUP_DIR -name "mysql_backup_*.sql" -mtime +7 -delete\n\
else\n\
    echo "Backup failed"\n\
    exit 1\n\
fi' > /usr/local/bin/backup.sh \
    && chmod +x /usr/local/bin/backup.sh

# 创建清理脚本
RUN echo '#!/bin/bash\n\
mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "CALL redfire_trading.CleanHistoricalData();"\n\
if [ $? -eq 0 ]; then\n\
    echo "Historical data cleanup completed"\n\
else\n\
    echo "Historical data cleanup failed"\n\
    exit 1\n\
fi' > /usr/local/bin/cleanup.sh \
    && chmod +x /usr/local/bin/cleanup.sh


# 暴露端口
EXPOSE 3306

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD /usr/local/bin/health-check.sh

# 设置工作目录
WORKDIR /var/lib/mysql

# 使用官方入口点
ENTRYPOINT ["docker-entrypoint.sh"]

# 启动命令 (简化，主要使用配置文件)
CMD ["mysqld"]
